package com.csd.view;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.csd.activity.MainActivity;
import com.csd.activity.MessageActivity;
import com.csd.activity.R;

public class MenuPopupwindow extends PopupWindow implements OnClickListener{

	  private View v;  
	  private Context context;
	 public MenuPopupwindow(final Activity context) {  
		 this.context = context;
	        LayoutInflater inflater = (LayoutInflater) context  
	                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
	        v = inflater.inflate(R.layout.pop_menu, null);  
	        int h = context.getWindowManager().getDefaultDisplay().getHeight();  
	        int w = context.getWindowManager().getDefaultDisplay().getWidth();  
	        // 设置SelectPicPopupWindow的View  
	        this.setContentView(v);  
	        // 设置SelectPicPopupWindow弹出窗体的宽  
	        this.setWidth(w / 2 + 50);  
	        // 设置SelectPicPopupWindow弹出窗体的高  
	        this.setHeight(LayoutParams.WRAP_CONTENT);  
	        // 设置SelectPicPopupWindow弹出窗体可点击  
	        this.setFocusable(true);  
	        this.setOutsideTouchable(true);  
	        // 刷新状态  
	        this.update();  
	        // 实例化一个ColorDrawable颜色为半透明  
	        ColorDrawable dw = new ColorDrawable(0000000000);  
	        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作  
	        this.setBackgroundDrawable(dw);  
	        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
	        // 设置SelectPicPopupWindow弹出窗体动画效果  
	        this.setAnimationStyle(R.style.AnimationPreview);  
	        TextView  tv_msg = (TextView) v.findViewById(R.id.msg);
	        TextView  tv_index  = (TextView) v.findViewById(R.id.index);
	        tv_msg.setOnClickListener(this);
	        tv_index.setOnClickListener(this);
	    }  
	  
	    /** 
	     * 显示popupWindow 
	     *  
	     * @param parent 
	     */  
	    public void showPopupWindow(View parent) {  
	        if (!this.isShowing()) {  
	            // 以下拉方式显示popupwindow  
	            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);  
	        } else {  
	            this.dismiss();  
	        }  
	    }

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.msg:
				Intent intent = new Intent(context, MessageActivity.class);
				context.startActivity(intent);
				this.dismiss();
				break;
			case R.id.index:
				Intent intent1 = new Intent(context, MainActivity.class);
				intent1.putExtra("num", 0);
				intent1.putExtra("flag", 0);
				context.startActivity(intent1);
				this.dismiss();
				break;

			default:
				break;
			}
		}  
	
}
